package cn.jetpiece.cloud.log.app.modular.log.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.jetpiece.cloud.core.page.PageFactory;
import cn.jetpiece.cloud.log.api.model.params.GatewayLogParam;
import cn.jetpiece.cloud.log.api.model.query.GatewayLogQuery;
import cn.jetpiece.cloud.log.api.model.result.GatewayLogResult;
import cn.jetpiece.cloud.log.app.modular.log.entity.GatewayLog;
import cn.jetpiece.cloud.log.app.modular.log.mapper.GatewayLogMapper;
import cn.jetpiece.cloud.log.app.modular.log.service.GatewayLogService;
import cn.jetpiece.cloud.log.app.modular.log.wrapper.GatewayLogWrapper;
import cn.jetpiece.cloud.model.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 网关日志 服务实现类
 * </p>
 *
 * @author XuJZ
 */
@Service
public class GatewayLogServiceImpl extends ServiceImpl<GatewayLogMapper, GatewayLog> implements GatewayLogService {

    @Override
    public boolean add(GatewayLogParam param) {
        GatewayLog bean = new GatewayLog();
        BeanUtil.copyProperties(param, bean);
        if (bean.getRequestTime() == null) {
            bean.setRequestTime(new Date());
        }
        return super.save(bean);
    }

    @Override
    public PageResult<GatewayLogResult> page(GatewayLogQuery param) {
        LambdaQueryWrapper<GatewayLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(GatewayLog::getId, GatewayLog::getUserId, GatewayLog::getUsername, GatewayLog::getTargetServer, GatewayLog::getRequestPath, GatewayLog::getRequestMethod, GatewayLog::getRequestSchema, GatewayLog::getIpAddress, GatewayLog::getRequestTime, GatewayLog::getResponseTime, GatewayLog::getExecuteTime);
        wrapper.eq(ObjectUtil.isNotEmpty(param.getId()), GatewayLog::getId, param.getId());
        wrapper.eq(ObjectUtil.isNotEmpty(param.getUserId()), GatewayLog::getUserId, param.getUserId());
        wrapper.eq(StrUtil.isNotBlank(param.getTargetServer()), GatewayLog::getTargetServer, param.getTargetServer());
        wrapper.like(StrUtil.isNotBlank(param.getRequestPath()), GatewayLog::getRequestPath, param.getRequestPath());
        wrapper.eq(StrUtil.isNotBlank(param.getRequestMethod()), GatewayLog::getRequestMethod, param.getRequestMethod());
        wrapper.eq(StrUtil.isNotBlank(param.getRequestSchema()), GatewayLog::getRequestSchema, param.getRequestSchema());
        wrapper.eq(StrUtil.isNotBlank(param.getIpAddress()), GatewayLog::getIpAddress, param.getIpAddress());
        wrapper.ge(ObjectUtil.isNotEmpty(param.getRequestTimeStart()), GatewayLog::getRequestTime, param.getRequestTimeStart());
        wrapper.lt(ObjectUtil.isNotEmpty(param.getRequestTimeEnd()), GatewayLog::getRequestTime, param.getRequestTimeEnd());
        wrapper.ge(ObjectUtil.isNotEmpty(param.getExecuteTimeStart()), GatewayLog::getExecuteTime, param.getExecuteTimeStart());
        wrapper.lt(ObjectUtil.isNotEmpty(param.getExecuteTimeEnd()), GatewayLog::getExecuteTime, param.getExecuteTimeEnd());
        if (param.getLogType() != null && param.getLogType().equals(1L)) {
            wrapper.and((subWrapper) -> {
                subWrapper.like(GatewayLog::getRequestPath, "login").or().like(GatewayLog::getRequestPath, "logout");
            });
        } else {
            wrapper.and((subWrapper) -> {
                subWrapper.notLike(GatewayLog::getRequestPath, "login").or().notLike(GatewayLog::getRequestPath, "logout");
            });
        }
        wrapper.orderByDesc(GatewayLog::getRequestTime);

        IPage<GatewayLog> iPage = PageFactory.defaultPage();
        IPage<GatewayLog> page = super.page(iPage, wrapper);
        return new GatewayLogWrapper(page).setWrapper(param.getWrapper()).wrapPageResult();
    }
}
